<?php defined('SYSPATH') or die('No direct script access.');

date_default_timezone_set("Europe/Belgrade");

class Controller_Home extends Controller_DefaultTemplate {

	private $message;
	/**
	 * Default controller. Shows the homepage
	 */
	public function action_index() {
		$keywords 			= array();
		$chartHistory 		= 7;
		$timeUnit 			= "DAY";
		$printableTimeUnit	= "dag";
		$dateTimeFormat		= "j M";
		
		if (isset($_GET['keywords'])) {
			$keywords 					= explode(' ', $_GET['keywords']);
		} if ((isset($_GET['chart-y-count'])) && ($_GET['chart-y-count'] > 2) && ($_GET['chart-y-count'] < 11)) {
			$chartHistory = $_GET['chart-y-count'];
		} if (isset($_GET['chart-time-unit'])) {
			switch($_GET['chart-time-unit']) {
				case "minuut":
					$timeUnit = "MINUTE";
					$printableTimeUnit	= "minuut";
					$dateTimeFormat		= "G:i";
					break;
				case "uur":
					$timeUnit = "HOUR";
					$printableTimeUnit	= "uur";
					$dateTimeFormat		= "G:i";
					break;
				case "dag":
					break;
				case "week":
					$timeUnit = "WEEK";
					$printableTimeUnit	= "week";
					$dateTimeFormat		= "W";
					break;
				case "maand":
					$timeUnit = "MONTH";
					$printableTimeUnit	= "maand";
					$dateTimeFormat		= "M";
					break;
				case "jaar":
					$timeUnit = "YEAR";
					$printableTimeUnit	= "jaar";
					$dateTimeFormat		= "o";
					break;
			}
		}
		$this->template->title 						= 'Home';
		$view 										= new View('home');
		$messages 									= new View('messages');
		$topAccounts								= new View('topAccounts');

		$model_message 								= new Model_Message;
		$model_tags									= new Model_Tags;
		$model_owner								= new Model_Owner;

		$this->template->content 					= $view;
		$this->template->pageTitle					= 'Dashboard';
		$this->template->script						= new View("defaults/googleScripts.php");
		$this->template->script->unit				= "Per ". $printableTimeUnit ." weergegeven"; 	// dag = j, maand = F, jaar = o.
		$this->template->script->date				= date("j-F-o G:i"); 								// dag = j, maand = F, jaar = o.
		$this->template->script->timeUnit			= strtolower($timeUnit);
		$this->template->script->dateTimeFormat 	= $dateTimeFormat;
		
		$this->template->chartOptions->select1		= $chartHistory;
		$this->template->chartOptions->select2		= $printableTimeUnit;
		
		$accountList = $model_owner->getTopOwners(10, $keywords);
		$topAccounts->bind('topOwners', $accountList);
		$view->bind('topAccounts', $topAccounts);
		
		$parameters = array(
			"history" => $chartHistory,
			"timeUnit" => $timeUnit,
			"labels" => $keywords
		);
		
		$this->template->script->data	= $model_tags->getTimelineTagData($parameters);

		$helper_tagcloud 			= new Helper_Tagcloud;
		$rst						= $model_tags->getTopTags($keywords);
		
		$baseUrl					= URL::base();
		$first						= true;
		
		foreach ($keywords as $keyword) {
			if ( $first ) {
				$baseUrl .= "?keywords=" . $keyword;
				$first = false;
			} else {
				$baseUrl .= "+" . $keyword;
			}
		}
			
		$count = 9;
		$url = "";
		foreach ($rst as $tag) {
		/*	
			if ( empty($keywords )) {
				$url = $baseUrl . "?keywords=" . $tag["name"];
			} else {
				$url = $baseUrl . "+" . $tag["name"];
			}
		*/	
			$helper_tagcloud->addWord(array('word' => $tag["name"], 'size' => $count, 'url' => $url));
			$count--;
		}

		$tagCloud					= $helper_tagcloud->showCloud();
		if($tagCloud == ""){
			$tagCloud = "<p>Er zijn geen tags om weer te geven.</p>";
		}
		$tweets						= $model_message->getNewMessages($keywords);

		$messages->bind("rst", $tweets);
		$view->bind("tweet", $messages);
		$view->bind("tagCloud", $tagCloud);
	}

}
